//
function openWindowWithPost(url,name,keys,values, option) {
	var newWindow;
	if(typeof  option == "undefined") {
		newWindow = window.open(url, name);
	} else {
		newWindow = window.open(url, name, option);
	}
	
	if (!newWindow) return false;
	var html = "";
	html += "<html><head>";
	html += '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'; 
	html += "</head><body><form id='formid' method='post' action='" + url + "'>";
	if (keys && values && (keys.length == values.length))
		for (var i=0; i < keys.length; i++)
			html += "<input type='hidden' name='" + keys[i] + "' value='" + values[i] + "'/>";
	html += "</form><script type='text/javascript'>document.getElementById(\"formid\").submit()</script></body></html>";
	
	newWindow.document.write(html);
	return newWindow;
}

function openWindowWithPost1(url,name,keys,values, option) {
	var html = "<form name='"+name+"' id='formid' method='post' target='"+name+"'>";
	if (keys && values && (keys.length == values.length))
		for (var i=0; i < keys.length; i++)
			html += "<input type='hidden' name='" + keys[i] + "' value='" + values[i] + "'/>";
	html += "</form>"
	var frm = $(html).get(0);
	frm.action = url;
	frm.target = name;
	
	if(typeof option == "undefined") {
		frm.onsubmit = function() {
			window.open(url, name);
		};
	} else {
		frm.onsubmit = function() {
			window.open(url, name, option);
		};
	}
	frm.submit();
}

function __afterTwitterSetting() {
}

bugs.personMedia = {
		layer : null,
		setting : function() {
			if(!g_isLogged) {
				bugs.ui.showLoginLayer();
				return;
			}
	
			var url = g_urlAjax+"/external/connectionManager";
			$.get(url, function(result) {
				if(result != "") {
					if(bugs.personMedia.layer != null) {
						bugs.personMedia.layer.remove();
					}
					bugs.personMedia.layer = $(result);
					bugs.personMedia.layer.css(
							{position : "absolute", left : "50%", top : 186, "margin-left" : -227}
					);
					bugs.personMedia.layer.appendTo("body");

					/*
					__afterTwitterSetting = function() {
						alert("");
						__afterTwitterSetting();
					};
					*/
				} else {
				}
			});
		},
		twitterAuth : function() {
			bugs.personMedia.close();
			bugs.twitter.popupAuth();
		},
		cancelTwitter : function() {
			var url = g_urlAjax+"/external/twitter/cancel";
			$.post(url, function(result) {
				if(result == "200") {

					$("#twitterAuthY").hide();
					$("#twitterAuthN").show();
					bugs.ui.showAlert("연동이 해제 되었습니다.", {css : "layerAdultNotice"});
				} else {
					bugs.ui.showAlert("연동 해제가 실패 하였습니다.", {css : "layerAdultNotice"});
				}
			});
		},
		close : function() {
			
			if(bugs.personMedia.layer != null) {
				bugs.personMedia.layer.hide();
				bugs.personMedia.layer.remove();
				
			}
		},
		log : function(cols, nums, scriptYn) {
			if(g_msrl != false) {
				if(typeof nums != "undefined") {
					nums[nums.length] = g_msrl;
				}
			}
			if(typeof scriptYn == "undefined") scriptYn = false;
			bugs.logging.add(bugs.twitter.logId, cols, nums, scriptYn);
		}
	}




bugs.twitter = {
	logId : 201,
	popupAuth : function(text, isSave) {
		if(typeof isSave == "undefinde") isSave = true;
	
		var url = g_urlWww + '/external/twitter/authorize?decorator=blank';
		if(isSave == false) {
			url = url + "&save=false";
		}
		if(!bugs.valid.empty(text)) {
			url = url + "&text=" + encodeURIComponent(text); 
			//var win = openWindowWithPost1(url, "_twitter", ["text"], [text], 'width=800,height=400,top=100,left=100,resizable=yes,scrollbars=no');
			//win.focus();
		} else {
//			var win = window.open(url,'_twitter','width=800,height=400,top=100,left=100,resizable=yes,scrollbars=no');
//			win.focus();
		}
		var win = window.open(url,'_twitter','width=1080,height=400,top=100,left=100,resizable=yes,scrollbars=no');
		win.focus();

	},
	popupStatus : function(text) {
		if(typeof text == "undefined") return;
		// () 때문에 IE에서 교차스크립트 방지를 위해서 페이지를 변경했다고 나와서 추가함
		text = text.replace(/\(/g, "<");
		text = text.replace(/\)/g, ">");
		var url =  "http://twitter.com/?status=" + encodeURIComponent(text);
		var win = window.open(url,'_twitterStatus','width=1080,height=600,top=100,left=100,resizable=yes,scrollbars=no');
		try { win.focus(); } catch (e) {}
	},
	checkAuth : function(success, fail) {
		var url = g_urlAjax+'/external/twitter/checkAuth';
		$.get(url, function(result) {
			bugs.log.log(result);
			if(typeof result !="undefined" && result == "1") {
				success();
			} else {
				fail();
			}
		});
	},
	textTemplate : '<h2 class="twitter">트위터로 보내기</h2>' +
		'<div class="sendCode">' +
		'	<p><%=text%></p>' +
		'</div>' +
		'<div class="btnArea">' +
		'	<span class="button typeME"><a href="javascript:void(0);">확인</a></span>' +		
		'	<span class="button typeM"><a href="javascript:void(0);">취소</a></span>' +
		'</div>',
	connectTemplate : '<p class="textAdminC">'+
		'	트위터 인증정보를 저장하시면<br />보다 쉽게 공유할 수 있습니다.'+
		'</p>'+
		'<div class="btnArea">'+
		'	<span class="button typeME"><a href="javascript:void(0);">확인</a></span>'+
		'	<span class="button typeM"><a href="javascript:void(0);">취소</a></span>'+
		'</div>',
	updateLayer : null,
	updateStatus : function(text) {
		bugs.twitter.popupStatus(text);
		return;
		if(!g_isLogged) {
			bugs.twitter.statusPopup(text, function() {
				bugs.twitter.popupAuth(text);
			});
			//bugs.twitter.popupStatus(text);
			return;
		} else {
			bugs.twitter.checkAuth(
				function() { // success
					bugs.twitter.statusPopup(text, function() {
						var updateUrl = g_urlAjax+"/external/twitter/updateStatus";
						$.post(updateUrl, {text : text}, function(result) {
							bugs.twitter.updateLayer.hide();
							bugs.twitter.updateLayer.remove();

							if(200 == result) {
								bugs.ui.showAlert("트위터에 보내기 성공 하였습니다.", {css : "layerAdultNotice"});
								return;
							} else {
								bugs.ui.showAlert("트위터에 보내기가 실패 하였습니다.<br/>잠시 후에 다시 시도하여 주세요.", {css : "layerAdultNotice"});
								return;
							}
							
						});
					});
				}, 
				function() { // fail
					bugs.twitter.connectPopup(text);
				}
			);			
		}
	},
	statusPopup : function(text, initOk) {
		var tmp = new bugs.template(bugs.twitter.textTemplate, {text : text});
		bugs.twitter.updateLayer = new bugs.ui.popup(tmp.html(), {
			title : "보내기", css : "layerSendContent" 
		});
		bugs.twitter.updateLayer.layer.find(".btnArea span:first a").click(function() {
			initOk();
			bugs.twitter.updateLayer.hide();
			bugs.twitter.updateLayer.remove();
		}).end().find(".btnArea span:last a").click(function() {
			bugs.twitter.updateLayer.hide();
			bugs.twitter.updateLayer.remove();
		});
		
		bugs.twitter.updateLayer.show();
	},
	popup : null,
	connectPopup : function(text) {
		var connectPopup = new bugs.ui.popup(bugs.twitter.connectTemplate, {
			title : "트위터 연동하기", css : "layerAdminMsg" 
		});
		bugs.twitter.popup = connectPopup;
		bugs.twitter.popup.layer.find(".btnArea span:first a").click(function() {
			bugs.twitter.popupAuth(text);
			bugs.twitter.popup.hide();
			bugs.twitter.popup.remove();
		}).end().find(".btnArea span:last a").click(function() {
			bugs.twitter.popupAuth(text, false);
			//bugs.twitter.popupStatus(text);
			bugs.twitter.popup.hide();
			bugs.twitter.popup.remove();
		});
		bugs.twitter.popup.show();
	},
	artist : function(artistId, artistNm) {
		//var url = bugs.url.artistPage(artistId);
		$.get(g_urlMusic + "/shortenurl/getUrl", {"type":"artist", "id":artistId}, function(result) {
 			var url = result;

			if(typeof url == "undefined") return;
			var text = url + " " + artistNm + " #벅스";
			bugs.twitter.updateStatus(text);
			bugs.personMedia.log(["twitter", "artist"], [artistId]);
		});
	},
	album : function(albumId, title, artistNm) {
		//var url = bugs.url.albumPage(albumId);
		$.get(g_urlMusic + "/shortenurl/getUrl", {"type":"album", "id":albumId}, function(result) {
			var url = result;
			
			if(typeof url == "undefined") return;
			var text = url + " " + title + " - " + artistNm + " #벅스";
			bugs.twitter.updateStatus(text);
			bugs.personMedia.log(["twitter", "album"], [albumId]);
		});
	},
	esAlbum : function(albumId, title){
		var url = bugs.url.esAlbumPage(albumId);
		if(typeof url == "undefined") return;
		var text = "[뮤직PD] " + title + " " + url + " #벅스";
		bugs.twitter.updateStatus(text);
		bugs.personMedia.log(["twitter", "esAlbum"], [albumId]);
	},
	openAlbum : function(albumId, title){
		var url = bugs.url.openAlbumPage(albumId);
		if(typeof url == "undefined") return;
		var text = "[공개앨범] " + title + " " + url + " #벅스";
		bugs.twitter.updateStatus(text);
		bugs.personMedia.log(["twitter", "openAlbum"], [albumId]);
	},	
	track : function(trackId, title, artistNm) {
		//var url = bugs.url.trackPage(trackId);
		$.get(g_urlMusic + "/shortenurl/getUrl", {"type":"track", "id":trackId}, function(result) {
			var url = result;
			
			if(typeof url == "undefined") return;
			var text = url + " " + title + " - " + artistNm + " #벅스";
			bugs.twitter.updateStatus(text);
			bugs.personMedia.log(["twitter", "track"], [trackId]);
		});
	},
	etc : function(url, title, eventId) {
//		var url = bugs.url.trackPage(trackId);
		if(typeof url == "undefined") return;
		var text = url + " " + title + " -  #벅스";
		bugs.twitter.updateStatus(text);
		bugs.personMedia.log(["twitter", "etc"], [eventId], true);
	},
	common : function(url, title) {
		if(typeof url == "undefined") return;
		var text = url + " " + title + " #벅스";
		bugs.twitter.updateStatus(text);
	},
	chart : function(chart, type, cn, date1, date2) {
		if(chart != "bugs") return;
		var text = "";
		var url = g_urlMusic + "/chart/" + chart + "/" + type + "?cn=" + cn;
		if(type=="realtime") {
			text = "벅스 실시간 차트("+date1+") " + url + " #벅스";
		} else if(type=="top1000") {
			text = "벅스 주간 차트("+date1+"~"+date2+" ) " + url + " #벅스";
		}
		bugs.twitter.updateStatus(text);
		bugs.personMedia.log(["twitter", "chart", type], [cn]);
	},
	cancel : function() {
	}
}

bugs.cyworld = {
		sid : "s0300048",
		popup : function(xu) {
			if(typeof xu == "undefined") return;
			var url = "http://api.cyworld.com/openscrap/post/v1/";
			url += "?xu=" + escape(xu);
			url += "&sid=" + bugs.cyworld.sid;
			var win = window.open(url, 'cyopenscrap', 'width=450,height=410');
			win.focus();
		},
		artist : function(artistId) {
			if(typeof artistId == "undefined") return;
			var xu = "http://www.bugs.co.kr/external/cyworld/artist/" + artistId;
			bugs.cyworld.popup(xu);
			bugs.personMedia.log(["cyworld", "artist"], [artistId]);
		},
		album : function(albumId) {
			if(typeof albumId == "undefined") return;
			var xu = "http://www.bugs.co.kr/external/cyworld/album/" + albumId;
			bugs.cyworld.popup(xu);
			bugs.personMedia.log(["cyworld", "album"], [albumId]);
		},
		track : function(trackId) {
			if(typeof trackId == "undefined") return;
			var xu = "http://www.bugs.co.kr/external/cyworld/track/" + trackId;
			bugs.cyworld.popup(xu);
			bugs.personMedia.log(["cyworld", "track"], [trackId]);
		},
		chart : function(chart, type, cn) {
			if(typeof type == "undefined") return;
			if(typeof cn == "undefined") return;
			var xu = "http://www.bugs.co.kr/external/cyworld/chart/" + type + "/" + cn;
			bugs.cyworld.popup(xu);
			bugs.personMedia.log(["cyworld", "chart", type], [cn]);
		},
		common : function(temporaryID, cn) {	// temporaryID = YYYYMMDDHH24MISS
			if(typeof temporaryID == "undefined") return;
			var xu = "http://www.bugs.co.kr/external/cyworld/common/" + temporaryID;
			if (typeof cn != "undefined")	xu = xu + "?cn=" + cn;
			bugs.cyworld.popup(xu);
		}
}

bugs.facebook = {
		popup : function(uu, tt) {
			if(typeof uu == "undefined") return;
			var url = "http://www.facebook.com/sharer.php";
			// () 때문에 IE에서 교차스크립트 방지를 위해서 페이지를 변경했다고 나와서 추가함
			tt = tt.replace(/\(/g, "<");
			tt = tt.replace(/\)/g, ">");
			url += "?u=" + escape(uu);
			if(typeof tt != "undefined")
				url += "&t=" + encodeURIComponent(tt);
			var win = window.open(url, 'facebook_share', 'width=772,height=450');
			win.focus();
		},
		popup2 : function(uu, tt) {
			if(typeof uu == "undefined") return;
			var url = "http://www.facebook.com/sharer.php";
			// () 때문에 IE에서 교차스크립트 방지를 위해서 페이지를 변경했다고 나와서 추가함
			tt = tt.replace(/\(/g, "<");
			tt = tt.replace(/\)/g, ">");
			url += "?u=" + encodeURIComponent(uu);
			if(typeof tt != "undefined")
				url += "&t=" + encodeURIComponent(tt);
			var win = window.open(url, 'facebook_share', 'width=772,height=450');
			win.focus();
		},
		artist : function(artistId, artistNm) {
			if(typeof artistId == "undefined") return;
			var uu = bugs.url.artistPage(artistId);
			//$.get(g_urlMusic + "/shortenurl/getUrl", {"type":"artist", "id":artistId}, function(result) {
			//	var uu = result;
				bugs.facebook.popup(uu, artistNm);
				bugs.personMedia.log(["facebook", "artist"], [artistId]);
			//});
		},
		album : function(albumId, title, artistNm) {
			if(typeof albumId == "undefined") return;
			//var uu = "http://www.bugs.co.kr/external/facebook/album/" + albumId;
			var uu = bugs.url.albumPage(albumId);
			//$.get(g_urlMusic + "/shortenurl/getUrl", {"type":"album", "id":albumId}, function(result) {
				//var uu = result;
				var tt = title + " - " + artistNm;
				bugs.facebook.popup(uu, tt);
				bugs.personMedia.log(["facebook", "album"], [albumId]);
			//});
		},
		esAlbum : function(albumId, title){
			if(typeof albumId == "undefined") return;
			var uu = bugs.url.esAlbumPage(albumId);
			var tt = "[뮤직PD] " + title;
			bugs.facebook.popup(uu, tt);
			bugs.personMedia.log(["facebook", "esAlbum"], [albumId]);
		},
		openAlbum : function(albumId, title){
			if(typeof albumId == "undefined") return;
			var uu = bugs.url.openAlbumPage(albumId);
			var tt = "[공개앨범] " + title;
			bugs.facebook.popup(uu, tt);
			bugs.personMedia.log(["facebook", "openAlbum"], [albumId]);
		},		
		track : function(trackId, title, artistNm) {
			if(typeof trackId == "undefined") return;
			var uu = bugs.url.trackPage(trackId);
			//$.get(g_urlMusic + "/shortenurl/getUrl", {"type":"track", "id":trackId}, function(result) {
				//var uu = result;
				var tt = title + " - " + artistNm;
				bugs.facebook.popup(uu, tt);
				bugs.personMedia.log(["facebook", "track"], [trackId]);
			//});
		},
		chart : function(chart, type, cn, date1, date2) {
			if(typeof type == "undefined") return;
			if(typeof cn == "undefined") return;
			var uu = g_urlMusic + "/chart/" + chart + "/" + type + "?cn=" + cn;
			var tt = "";
			if(type=="realtime") {
				tt = "벅스 실시간 차트("+date1+")";
			} else if(type=="top1000") {
				tt = "벅스 주간 차트("+date1+"~"+date2+" )";
			}

			bugs.facebook.popup(uu, tt);
			bugs.personMedia.log(["facebook", "chart", type], [cn]);
		},
		common : function(url, title) {
			bugs.facebook.popup(url, title);
		}
}

bugs.me2day = {
	popupStatus : function(text, tags) {
		if(typeof text == "undefined") return;
		var url =  "http://me2day.net/posts/new?new_post[body]=" + encodeURIComponent(text);
		var tagString = "bugs";
		if(typeof tags != "undefined" && tags.length > 0) {
			for(var i=0;i < tags.length; i++) {
				if(typeof tags[i] != "undefined" && tags[i] != "")
					tagString = tagString + " " + tags[i].replace(/ /g, "");
			}
		}
		if(tagString != "")
			url = url + "&new_post[tags]=" + encodeURIComponent(tagString);
		var win = window.open(url,'me2dayPost','width=1030,height=600,top=100,left=100,resizable=yes,scrollbars=yes');
		win.focus();
	},
	updateStatus : function(text, tags) {
		bugs.me2day.popupStatus(text, tags);
		return;
	},
	artist : function(artistId, artistNm) {
		//var url = bugs.url.artistPage(artistId);
		$.get(g_urlMusic + "/shortenurl/getUrl", {"type":"artist", "id":artistId}, function(result) {
			var url = result;
			if(typeof url == "undefined") return;
			var text = '"' + artistNm + '":' + url + "  " + url + " ";
			bugs.me2day.updateStatus(text, [artistNm]);
			bugs.personMedia.log(["me2day", "artist"], [artistId]);
		});
	},
	album : function(albumId, title, artistId, artistNm) {
		//var url = bugs.url.albumPage(albumId);
		$.get(g_urlMusic + "/shortenurl/getUrl", {"type":"album", "id":albumId}, function(result) {
			var url = result;
			if(typeof url == "undefined") return;
			//var artistUrl = bugs.url.artistPage(artistId);
			$.get(g_urlMusic + "/shortenurl/getUrl", {"type":"artist", "id":artistId}, function(result2) {
				var artistUrl = result2;
				var text = "\"" + title + '":' + url  + "  - \"" + artistNm + '":' + artistUrl + "  " + url;
				bugs.me2day.updateStatus(text, [title, artistNm]);
				bugs.personMedia.log(["me2day", "album"], [albumId]);
			});
		});
	},
	esAlbum : function(albumId, title, pdNm){
		var url = bugs.url.esAlbumPage(albumId);
		if(typeof url == "undefined") return;
		var text = "\"[뮤직PD] " + title + "\" : " + url;
		bugs.me2day.updateStatus(text, [title, pdNm]);
		bugs.personMedia.log(["me2day", "esAlbum"], [albumId]);
	},
	openAlbum : function(albumId, title, pdNm){
		var url = bugs.url.openAlbumPage(albumId);
		if(typeof url == "undefined") return;
		var text = "\"[공개앨범] " + title + "\" : " + url;
		bugs.me2day.updateStatus(text, [title, pdNm]);
		bugs.personMedia.log(["me2day", "openAlbum"], [albumId]);
	},	
	track : function(trackId, title, artistId, artistNm) {
		//var url = bugs.url.trackPage(trackId);
		$.get(g_urlMusic + "/shortenurl/getUrl", {"type":"track", "id":trackId}, function(result) {
			var url = result;
			if(typeof url == "undefined") return;
			
			//var artistUrl = bugs.url.artistPage(artistId);
			$.get(g_urlMusic + "/shortenurl/getUrl", {"type":"artist", "id":artistId}, function(result2) {
				var artistUrl = result2;
				var text = "\"" + title + '":' + url  + "  - \"" + artistNm + '":' + artistUrl + "  " + url;
				bugs.me2day.updateStatus(text, [title, artistNm]);
				bugs.personMedia.log(["me2day", "track"], [trackId]);
			});
		});
	}, 
	chart : function(chart, type, cn, date1, date2) {
		if(chart != "bugs") return;
		var text = "";
		var url = g_urlMusic + "/chart/" + chart + "/" + type + "?cn=" + cn;
		var tag = "";
		if(type=="realtime") {
			text = "\"벅스 실시간 차트("+date1+")\":" + url + " ";
			tag = "벅스 실시간 차트";
		} else if(type=="top1000") {
			text = "\"벅스 주간 차트("+date1+"~"+date2+" )\":" + url + " ";
			tag = "벅스 주간 차트";
		}
		bugs.me2day.updateStatus(text, [tag]);
		bugs.personMedia.log(["me2day", "chart", type], [cn]);
	},
	common : function(title, url, tags) {
		if(typeof url == "undefined") return;
		var text = title + " :" + url + " ";
		bugs.me2day.updateStatus(text, tags);
	}
}


bugs.personEvent = {
	eventLogId : 210,
	log : function(cols, nums) {
		$('.layerSNSEvent').show();
		
		if(g_msrl != false) {
			if(typeof nums != "undefined") {
				nums[nums.length] = g_msrl;
			}
		}
		bugs.logging.add(this.eventLogId, cols, nums, true);
	}
}